<?php
namespace app\api\controller;

use app\common\controller\IndexBaseController;
use think\Db;
use app\admin\model\VerifyCode as VerifyCodeMode;
class SystemController extends IndexBaseController {
	protected function _initialize() {
			 parent::_initialize();
			header('Access-Control-Allow-Origin:*');
	}
	/*
	 * API短信 等系统通用接口
	 */
    public function index() {
    	return json(['msg'=>'this is API V1.0']);
    }
	
   
	
	/**
	 * 获得注册验证码
	 */
	public function get_login_sms(){
		return $this ->get_code(input('mobile'),2);
	}
	/**
	 * 获取openid
	 */
	public function getcode(){
		$we_config = get_config('payment_config');
//		define("APPID", $we_config['wxpay_appid']);
//		define("MCHID", $we_config['wxpay_mchid']);
//		define("KEY", $we_config['wxpay_key']);
//		define("APPSECRET", $we_config['wxpay_appsercet']);
		$code = $_GET['code'];//获取code
		$weixin =  file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$we_config['wxpay_appid']."&secret=".$we_config['wxpay_appsercet']."&code=".$code."&grant_type=authorization_code");//通过code换取网页授权access_token
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		$openid = $array['openid'];//输出openid
		return $openid;
	}
	/**
	 * 获得注册验证码
	 */
	public function return_pwd_sms(){
		return $this ->get_code(input('mobile'),3);
	}
	/**
	 * 获取分销设置
	 */
	public function getDisbution(){
		return $this->toJson(200, 'ok', getSiteBname("distribution"));
	}
	/**
	 * 上传图片
	 */
	public function upload() {
			$ret = array('strings' => $_POST, 'error' => '0');
			$fs = array();
			foreach ($_FILES as $name => $file) {
				$fn = $file['name'];
				$ft = strrpos($fn, '.', 0);
				$fm = substr($fn, 0, $ft);
				$fe = substr($fn, $ft);
				$fn = time()."_".rand(100000, 999999) . $fe;
				$fp = './public/uploads/image/'. $fn;
				//move_uploaded_file($file['tmp_name'], $fp);
				$image = \image\Image::open($file['tmp_name']);
				$image->thumb(100, 100)->save($fp);
				$fs[$name] = array('name' => $fn, 'url' => $fp, 'type' => $file['type'], 'size' => $file['size']);
			}
			$ret['files'] = $fs;
			echo json_encode($ret);
		
		exit;
	}
}
